package com.jtlrm.ckd.mvp.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.sdk.app.GlideApp;
import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.DeviceUtil;
import com.base.sdk.util.ImageUtil;
import com.base.sdk.widget.TitleBar;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.fragment.BaseFragment;
import com.jtlrm.ckd.entity.NewListEntity;
import com.jtlrm.ckd.entity.event.ShenHeUpdate;
import com.jtlrm.ckd.entity.event.SuiFangUpdateMessage;
import com.jtlrm.ckd.mvp.model.HuanZheGuanLiModel;
import com.jtlrm.ckd.mvp.model.NewsModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.mvp.view.activity.HomeQueryHuanZheActivity;
import com.jtlrm.ckd.mvp.view.activity.MyQRCodeActivity;
import com.jtlrm.ckd.mvp.view.activity.NewHuanZheJianDangActivity;
import com.jtlrm.ckd.mvp.view.activity.NewsDetailActivity;
import com.jtlrm.ckd.mvp.view.activity.ShuJuShenHeActivity;
import com.jtlrm.ckd.mvp.view.activity.SuiFangPaiBanActivity;
import com.jtlrm.ckd.util.EventBusUtil;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;


/**
 * 首页
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener {


    @BindView(R.id.home_banner)
    Banner banner;
    @BindView(R.id.suifang_sum)
    TextView suiFangT;
    @BindView(R.id.shenhe_sum)
    TextView shenHeT;
    @BindViews({R.id.home_1, R.id.home_2, R.id.home_3, R.id.home_4, R.id.home_5})
    List<LinearLayout> homeItems;
    @BindView(R.id.huanzhe_jiandang)
    LinearLayout huanzheJianDang;
    NewsModel newsModel;
    List<NewListEntity> newListEntities;
    HuanZheGuanLiModel huanZheGuanLiModel;
    @BindView(R.id.huanzhe_jiandang_icon)
    ImageView huanZheIcon;
    @BindView(R.id.home_huanzhe_label)
    TextView huanZheLabel;
    @BindView(R.id.home_huanzhe_description)
    TextView huanZheDescription;
    @BindView(R.id.home_huanzhe_kuaisu)
    TextView huanZheFast;
    @BindView(R.id.home_huanzhe_gaoxiao)
    TextView huanZheGaoxiao;

    @BindView(R.id.title_right_image)
    ImageView titleRightI;

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        if (UserHelper.getInstance(context).isDoctor()) {
            titleRightI.setImageResource(R.drawable.er_wei_ma_icon);
            titleRightI.setVisibility(View.VISIBLE);
            titleRightI.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(context, MyQRCodeActivity.class));
                }
            });
        } else {
            huanZheIcon.setImageResource(R.drawable.home_bottom_icon_grey);
            huanZheLabel.setTextColor(getResources().getColor(R.color.text_not_color));
            huanZheDescription.setTextColor(getResources().getColor(R.color.text_not_color));
            huanZheFast.setTextColor(getResources().getColor(R.color.text_not_color));
            huanZheGaoxiao.setTextColor(getResources().getColor(R.color.text_not_color));
            huanZheFast.setBackground(getResources().getDrawable(R.drawable.grey_not_text_background));
            huanZheGaoxiao.setBackground(getResources().getDrawable(R.drawable.grey_not_text_background));
            int padding = DeviceUtil.dip2px(context, 10);
            huanZheFast.setPadding(padding, 0, padding, 0);
            huanZheGaoxiao.setPadding(padding, 0, padding, 0);
            titleRightI.setVisibility(View.GONE);
        }
        newsModel = new NewsModel();
        huanZheGuanLiModel = new HuanZheGuanLiModel();
        EventBusUtil.register(this);
    }

    @Override
    protected void obtainData() {
        int screeWith = DeviceUtil.getScreenWidth(context);
        int bannerHeight = (int) ((680 * 1.0f / 1080) * screeWith);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) banner.getLayoutParams();
        layoutParams.height = bannerHeight;
        banner.setLayoutParams(layoutParams);
        banner.setImageLoader(new GlideImageLoader());
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (newListEntities != null && newListEntities.size() > position) {
                    NewListEntity entity = newListEntities.get(position);
                    NewsDetailActivity.goDetail(context, entity.getLinkUrl());
                }
            }
        });
        queryNews();
        querySuiFangAndShenHe();
    }

    private void querySuiFangAndShenHe() {
        querySuiFang();
        queryShenHeRenShu();
    }

    public void querySuiFang() {
        huanZheGuanLiModel.querySuiFangRenShu(new CommonObserver<String>() {
            @Override
            public void onError(int errType, String errMessage) {
                String info = "0人";
                SpannableString spannableString = new SpannableString(info);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F7A98B")), 0, info.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                suiFangT.setText(spannableString);
            }

            @Override
            public void onResult(String data) {
                String info = data + "人";
                SpannableString spannableString = new SpannableString(info);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F7A98B")), 0, info.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                suiFangT.setText(spannableString);
            }
        }, lifecycleSubject);
    }

    public void queryShenHeRenShu() {
        huanZheGuanLiModel.queryShenHeRenShu(new CommonObserver<String>() {
            @Override
            public void onError(int errType, String errMessage) {
                String info = "0条";
                SpannableString spannableString = new SpannableString(info);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F7A98B")), 0, info.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                shenHeT.setText(spannableString);
            }

            @Override
            public void onResult(String data) {
                String info = data + "条";
                SpannableString spannableString = new SpannableString(info);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F7A98B")), 0, info.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                shenHeT.setText(spannableString);
            }
        }, lifecycleSubject);
    }

    private void queryNews() {
        newsModel.queryHomeNews(new CommonObserver<List<NewListEntity>>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast("录播图" + errMessage);
            }

            @Override
            public void onResult(List<NewListEntity> data) {
                newListEntities = data;
                banner.setImages(data);
                banner.start();
            }
        }, lifecycleSubject);
    }

    @Override
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initEvent() {
        for (LinearLayout linearLayout : homeItems) {
            linearLayout.setOnClickListener(this);
        }
        huanzheJianDang.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_1:
                startActivity(new Intent(context, SuiFangPaiBanActivity.class));
                break;
            case R.id.home_2:
                startActivity(new Intent(context, ShuJuShenHeActivity.class));
                break;
            case R.id.home_3:
                HomeQueryHuanZheActivity.goSearch(context, 3);
                break;
            case R.id.home_4:
                HomeQueryHuanZheActivity.goSearch(context, 4);
                break;
            case R.id.home_5:
                HomeQueryHuanZheActivity.goSearch(context, 5);
                break;
            case R.id.huanzhe_jiandang:
                if (UserHelper.getInstance(context).isDoctor()) {
                    startActivity(new Intent(context, NewHuanZheJianDangActivity.class));
                } else {
                    showToast("当前仅支持医生建档");
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SuiFangUpdateMessage message) {
        if (message != null) {
            querySuiFang();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ShenHeUpdate message) {
        if (message != null) {
            queryShenHeRenShu();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }


    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            GlideApp.with(imageView.getContext()).load(((NewListEntity) path).getImageUrl()).into(imageView);
        }
    }
}
